package com.irdstudio.rsp.drools.common;

import org.kie.api.runtime.rule.AgendaFilter;
import org.kie.api.runtime.rule.Match;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 根据规则id集合过滤规则执行结果
 */
public class BizRuleAgendaFilter implements AgendaFilter {

    private static final Logger log = LoggerFactory.getLogger(BizRuleAgendaFilter.class);

    private Set<String> ruleSet;

    public BizRuleAgendaFilter() {
        this.ruleSet = new LinkedHashSet<>();
    }

    public BizRuleAgendaFilter(Collection<String> set) {
        if (set == null) {
            throw new IllegalArgumentException("set不能为空");
        }
        this.ruleSet = new LinkedHashSet<>(set.size());
        this.ruleSet.addAll(set);
    }

    public Set<String> getRuleSet() {
        return ruleSet;
    }


    @Override
    public boolean accept(Match match) {
        String ruleId = match.getRule().getName();
        if (this.ruleSet.contains(ruleId)) {
            if (log.isDebugEnabled()) {
                log.debug("执行规则 {}", ruleId);
            }
            return true;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("跳过执行规则 {}", ruleId);
            }
            return false;
        }
    }
}
